/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package provider.impl.barratt;

import barratt.BarrattModelData;
import barratt.provider.Providers;
import commons.provider.impl.BaseXMLFileBasedProviderImpl;
import org.dom4j.Element;

import java.util.List;

/**
 * @author Susanta Tewari
 * @version 1.0.0
 * @history First created on 04/12/2012.
 * @since 1.0.0
 */
public class ProviderImpls {

    /**
     *
     */
    public static final class XMLBarratModelDataProviderImpl
            extends BaseXMLFileBasedProviderImpl<BarrattModelData>
            implements Providers.BarratModelDataProviderFileBased {

        /**
     * {@inheritDoc} {@code dataFile} must conform to the specified xml <a href="tracker/cef/pui/pob/doc-files/MoneyBudget.xml.txt">format</a>,
     * and accompany its <a href="tracker/cef/pui/pob/doc-files/Budget.xsd.txt">schema</a>.
     */
        @Override
        protected BarrattModelData toValue(final Element root)
                throws commons.provider.ProviderException {


            // 4. get 'event' elements from the root 'datums'
            final List<Element> datums             = root.elements("datum");
            final BarrattModelData barratModelData = new BarrattModelData();

            for (final Element datum : datums) {

                final String physical_distance = datum.elementText("physical-distance");
                final String recom_distance    = datum.elementText("recom-distance");

                barratModelData.addData(physical_distance, recom_distance);
            }

            return barratModelData;
        }
    }
}
